package com.dzu.medium;

/**
 * @author ZhaoDong
 * @date 2022/12/22 10:45
 * @description 50. Pow(x, n)
 */
public class Test50 {
    public static void main(String[] args) {
        System.out.println(myPow(2.0, 3));
    }

    /**
     * https://leetcode.cn/problems/powx-n/solution/50-powx-n-kuai-su-mi-qing-xi-tu-jie-by-jyd/
     *
     * @param x
     * @param n
     * @return
     */
    public static double myPow(double x, int n) {
        if (x == 0.0f) {
            return 0.0d;
        }
        long b = n;
        double res = 1.0;
        if (b < 0) {
            x = 1 / x;
            b = -b;
        }
        while (b > 0) {
            if ((b & 1) == 1) {
                res *= x;
            }
            x *= x;
            b >>= 1;
        }
        return res;
    }
}
